package com.yt.file.api;

import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.file.models.service.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件API控制器（仅提供上传接口）
 */
@RestController
@RequestMapping("/api/file")
@Api(tags = "文件API")
public class FileApiController {
    private static final Logger logger = LoggerFactory.getLogger(FileApiController.class);

    @DubboReference
    private IFileService fileService;

    /**
     * 上传单张图片
     */
    @PostMapping("/upload/image")
    @ApiOperation(value = "上传单张图片")
    public Result<String> uploadImage(@RequestParam("file") MultipartFile file) {
        try {
            logger.info("开始上传单张图片");
            String imageUrl = fileService.uploadImage(file);
            logger.info("单张图片上传成功，url: {}", imageUrl);
            return new Result<>(CodeEnum.SUCCESS, imageUrl);
        } catch (Exception e) {
            logger.error("单张图片上传失败", e);
            return new Result<>(CodeEnum.FAIL_100006);
        }
    }

    /**
     * 上传多张图片
     */
    @PostMapping("/upload/images")
    @ApiOperation(value = "上传多张图片")
    public Result<List<String>> uploadImages(@RequestParam("files") List<MultipartFile> files) {
        try {
            logger.info("开始上传多张图片，数量: {}", files.size());
            List<String> imageUrls = fileService.uploadImages(files);
            logger.info("多张图片上传成功，数量: {}", imageUrls.size());
            return new Result<>(CodeEnum.SUCCESS, imageUrls);
        } catch (Exception e) {
            logger.error("多张图片上传失败", e);
            return new Result<>(CodeEnum.FAIL_100006);
        }
    }
}